package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.demand.PaymentRecordGroup;
import com.eastfair.pay.dto.PaymentRecordDTO;
import com.eastfair.pay.dto.PaymentRecordPageQuery;
import com.eastfair.pay.dto.PaymentRecordSaveDTO;
import com.eastfair.pay.dto.PaymentRecordUpdateDTO;
import com.eastfair.pay.entity.PaymentRecord;
import com.eastfair.pay.service.PaymentRecordService;
import com.eastfair.pay.vo.PaymentRecordVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 付款记录
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/paymentRecord")
@Api(value = "PaymentRecord", tags = "付款记录")
@PreAuth(replace = "pay:paymentRecord:")
public class PaymentRecordController extends SuperController<PaymentRecordService, Long, PaymentRecord, PaymentRecordPageQuery, PaymentRecordSaveDTO, PaymentRecordUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<PaymentRecord> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<PaymentRecord> paymentRecordList = list.stream().map((map) -> {
            PaymentRecord paymentRecord = PaymentRecord.builder().build();
            //TODO 请在这里完成转换
            return paymentRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(paymentRecordList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<PaymentRecordPageQuery> params) {
        PaymentRecordPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    @PostMapping("/updatePaymentRecord")
    public R update(@Validated @RequestBody PaymentRecordUpdateDTO paymentRecordUpdateDTO){
        try {
            baseService.update(paymentRecordUpdateDTO);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    @PostMapping("/batchSave")
    public R batchSave(@Validated @RequestBody List<PaymentRecordSaveDTO> dtos){
        try {
            baseService.batchSave(dtos);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }
    @PostMapping("/queryPagePaymentRecord")
    @BaseControllerInfo(voGroup = PaymentRecordGroup.queryPagePaymentRecord.class)
    public R<Page<PaymentRecordVO>> queryPage(@RequestBody @Validated PageParams<PaymentRecordPageQuery> params){
        try {
            Page<PaymentRecordVO> page = baseService.queryPagePaymentRecord(params);
            return R.success(page);
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 合同认领展示付款计划
     * @param paymentRecordDTO
     * @return
     */
    @PostMapping("/queryPaymentRecordList")
//    @BaseControllerInfo(voGroup = PaymentRecordGroup.queryPaymentRecordList.class)
    public R<List<PaymentRecordVO>> queryList(@RequestBody PaymentRecordDTO paymentRecordDTO){
        try {
            List<PaymentRecord> paymentRecordList = baseService.queryList(paymentRecordDTO);
            List<PaymentRecordVO> records = ConvertUtil.convertList(paymentRecordList, PaymentRecordVO.class);
            return R.success(records);
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    @GetMapping("/deleteById/{id}")
    public R delete(@PathVariable("id") Long id){
        try {
            baseService.deleteById(id);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    @PostMapping("/deleteByMainno")
    public R deleteByMainno(@RequestBody PaymentRecordDTO paymentRecordDTO){
        try {
            baseService.deleteByMainno(paymentRecordDTO.getMainno());
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 结算认领
     * @param paymentRecordDTO paymentRecordDTO
     * @return
     */
    @PostMapping("/settlementCollect")
    public R<Boolean> settlementCollect(@RequestBody PaymentRecordDTO paymentRecordDTO){
        try {
            return R.success(baseService.settlementCollect(paymentRecordDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * @Author clm
     * @Description //金额认领
     * @Date 15:20 2023/2/14
     * @Param [paymentRecordDTO]
     * @return com.eastfair.core.base.R<java.lang.Boolean>
     **/
    @PostMapping("/amountCollect")
    public R<Boolean> amountCollect(@RequestBody PaymentRecordDTO paymentRecordDTO){
        try {
            baseService.amountCollect(paymentRecordDTO);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

}
